home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / board / MigaMind.lha / MigaMind / main.c < prev    next >
C/C++ Source or Header  |  1990-08-11  |  7KB  |  289 lines

  1. /****************************************************************************
  2.  
  3.                 Main Object of:
  4.                 -= Miga Mind =-
  5.                                 1-5-90
  6.                                  Ekke
  7. ****************************************************************************/
  8.  
  9. #include <exec/types.h>
  10. #include <intuition/intuition.h>
  11. #include <graphics/gfxbase.h>
  12. #include <graphics/gfxmacros.h>
  13. #include <stdio.h>
  14. #include <proto/all.h>
  15. #include "Gads.h"
  16. #include "MM_Proto.h"
  17.  
  18. #define VERSION     "MigaMind v1.0\nEkke Verheul\n"
  19. #define DATE         "dd 1-5-'90\n"
  20. #define IDCMP_FLAG     MOUSEBUTTONS
  21. #define WB_OUTFILE     "CON:10/10/200/40/ MigaMind "
  22. #define XPOS         (Window->LeftEdge)
  23. #define YPOS        (Window->TopEdge)
  24. #define M_WIDTH        (Window->WScreen->Width - Window->Width)
  25. #define M_HEIGHT    (Window->WScreen->Height - Window->Height)
  26. #define SMX        (Window->WScreen->MouseX)
  27. #define SMY        (Window->WScreen->MouseY)
  28.  
  29. struct  NewWindow     NewWindow =    
  30.     { 271,20,98,153,-1,-1,IDCMP_FLAG,        
  31.     SMART_REFRESH|BORDERLESS|RMBTRAP|ACTIVATE|REPORTMOUSE,
  32.     NULL,NULL,NULL,NULL,NULL,-1,-1,-1,-1,WBENCHSCREEN };
  33.  
  34. struct GfxBase         *GfxBase     = NULL;
  35. struct IntuitionBase     *IntuitionBase     = NULL;
  36. struct Window        *Window        = NULL;
  37.  
  38. int CXBRK(void) {  return(0); }
  39.  
  40. /* only this: */
  41. BYTE Value[46] ;
  42.  
  43.  
  44. /*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  45. :::::::::::: void MAIN() :::::::::::::::::::::::::::::::::::::::::::::::::::
  46. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  47. void main(int argc,char **argv)
  48. {
  49. BOOL     buzy = 2;
  50. struct     IntuiMessage *message ;
  51. USHORT     code,x,y;
  52. ULONG     class;
  53. int     fromwb = 0;
  54. int     lastsys = -1,turn,i;
  55. int     sel[2] = { -1,-1 };
  56. int     colors,positions,ready;
  57. BYTE     MMCode[4];
  58.  
  59. /* WORKBENCH ? */
  60.     if (argc == 0) fromwb = 1;
  61.  
  62. /* VERSION */
  63.     if ((argc > 1) && (*argv[1] == '?')) Nop("?",fromwb);
  64.  
  65. /* LIBRARIES:  */ 
  66.     if ((IntuitionBase = (struct IntuitionBase *) OpenLibrary
  67.                 ("intuition.library",0 )) == 0 )      
  68.                 Nop("intuition.library not found.",fromwb);
  69.     if ((GfxBase = (struct GfxBase *)OpenLibrary
  70.                 ("graphics.library",0)) == NULL)
  71.                 Nop("graphics.library not found.",fromwb);
  72. /* WINDOW */
  73.     if ((Window = OpenWindow (&NewWindow)) == NULL )
  74.                 Nop("Can't open Window.\n",fromwb);
  75.     
  76. /* LETS GET TO WORK */
  77.     while(buzy)
  78.     {   if (buzy == 2)    /* start (again) */
  79.         {   LayOut(Window->RPort);
  80.         for (i=0; i<46; i++)
  81.         {    if (i<6) Value[i] = i;
  82.             else Value[i] = -1;
  83.         }
  84.         GetCode(MMCode);
  85.         turn = 1;
  86.         buzy = 1;
  87.         ready = 0;
  88.         }
  89.         else if (buzy == 3) /* show hidden code */
  90.         {   if (CheckValue(turn))
  91.         {   colors = WrightColor(turn,MMCode);
  92.             positions = WrightPosition(turn,MMCode);
  93.             PaintWright(Window->RPort,turn,colors,positions);
  94.             if (positions == 4 || ++turn > 10) 
  95.             {    ready = 1;
  96.             DrawSysGad(Window->RPort,1,0,1);
  97.             ShowAnswer(Window->RPort,MMCode);
  98.             DisplayBeep(Window->WScreen);
  99.             }
  100.         }
  101.         buzy = 1;
  102.         }
  103.  
  104.         Wait (1<<Window->UserPort->mp_SigBit);
  105.             while (message= (struct IntuiMessage *)GetMsg(Window->UserPort)) 
  106.             {    class = message->Class  ;
  107.         code  =    message->Code   ;
  108.                 x     = message->MouseX ;
  109.                 y     = message->MouseY ;
  110.             ReplyMsg((struct Message *)message);
  111.  
  112.                 switch(class)
  113.                 {   case MOUSEBUTTONS:
  114.                     {   if (code == SELECTDOWN)
  115.             {   if (y < 136 && ready == 0) 
  116.                 {    HandleSD(Window->RPort,x,y,turn,sel);
  117.                 }
  118.                 else lastsys = HandleSysD(Window->RPort,x,y,ready);
  119.             }
  120.                         else if (code == SELECTUP)
  121.             {   buzy = HandleSysU(Window->RPort,x,y,lastsys,ready);
  122.                 lastsys = -1;
  123.             }
  124.                         else if (code == MENUDOWN)
  125.                 {   if ((x = SMX) > M_WIDTH) x = M_WIDTH;
  126.                 if ((y = SMY) > M_HEIGHT) y = M_HEIGHT;
  127.                 x -= XPOS;
  128.                 y -= YPOS;
  129.                 MoveWindow(Window,x,y);
  130.             }
  131.     }   }    }   }
  132.     Nop(NULL,fromwb);
  133. }/* end of main() */
  134.             /******************************************************
  135. *******************/    /* close-all & err-mess */
  136. void Nop(char *err,int fromwb)
  137. {
  138. FILE *outfile;
  139.  
  140.     if (Window) CloseWindow (Window);
  141.     if (err && *err)
  142.     {   if (fromwb) 
  143.         {    outfile = fopen(WB_OUTFILE,"r+");
  144.         }
  145.         else
  146.         {    outfile = fopen("*","r+");
  147.         }
  148.         if (NULL != outfile) 
  149.         {    if (*err == '?')
  150.             {   fprintf(outfile, VERSION );
  151.                 fprintf(outfile, DATE    );
  152.             }
  153.             else
  154.             {   fprintf(outfile,"%s\n",err);
  155.             }
  156.             if (fromwb) Delay(300);
  157.             fclose(outfile);
  158.         }
  159.     }
  160.     if (GfxBase)        CloseLibrary((struct Library *)GfxBase);
  161.     if (IntuitionBase)    CloseLibrary((struct Library *)IntuitionBase);
  162.     XCEXIT(0);
  163. }
  164.  
  165. /********************************* SELECT-DOWN ****************************/
  166. int HandleSD(struct RastPort *rp,WORD x,WORD y,int turn,int *sel)
  167. int t,id;
  168.  
  169.     t = GetGadTurn(y);
  170.     if (turn != -1) id = GetGadID(t,x);
  171.     if (t == -1 || id == -1) return(0);
  172.  
  173.     if (t < turn) 
  174.     {   if (sel[0] != -1)
  175.         {    DrawGad(rp,sel[0],sel[1],Value[GadNr(sel[0],sel[1])],0);
  176.         }
  177.         DrawGad(rp,t,id,Value[GadNr(t,id)],1);
  178.         sel[0] = t;
  179.         sel[1] = id;
  180.     }
  181.     else if (t == turn && sel[0] != -1)
  182.     {   DrawGad(rp,sel[0],sel[1],Value[GadNr(sel[0],sel[1])],0);
  183.         DrawGad(rp,t,id,Value[GadNr(sel[0],sel[1])],0);
  184.         Value[GadNr(t,id)] = Value[GadNr(sel[0],sel[1])];
  185.         sel[0] = -1;
  186.     }
  187.     return(0); 
  188. }
  189.  
  190. /********************************* SYSTEM-DOWN *****************************/
  191. int HandleSysD(struct RastPort *rp,WORD x,WORD y,int w)
  192. int id;
  193.  
  194.     id = GetSysID(x,y);
  195.     if (id != -1) DrawSysGad(rp,id,1,w);
  196.     return(id); 
  197. }
  198.  
  199. /********************************* SYSTEM-UP *******************************/
  200. int HandleSysU(struct RastPort *rp,WORD x,WORD y,int lastid,int w)
  201. {
  202.     if (lastid != -1) DrawSysGad(rp,lastid,0,w);
  203.     if (lastid == GetSysID(x,y)) 
  204.     {   switch(lastid)
  205.         {    case QUIT:
  206.         {    return(0);
  207.         }
  208.         case SHOW:
  209.         {   if (w) return(2);
  210.             else return(3);
  211. }    }   }    }
  212. /******************************** RANDOM ***********************************/
  213. void GetCode(BYTE *r)
  214. {
  215. unsigned int clock[2];
  216. int i,j,same;
  217.  
  218.     timer(clock);
  219.     srand(clock[0]*clock[1]);
  220.     for (i=0; i<4; i++)
  221.     {   same = 1;
  222.         while (same)
  223.         {    r[i] = rand() % 6;
  224.         same = 0;
  225.         for (j=0; j<i; j++)
  226.             {   if (r[i] == r[j])
  227.             {    same = 1;
  228.     }   }    }   }
  229. }
  230. /***************************************************************************/
  231. int CheckValue(int turn)
  232. {
  233. int i;
  234.     for (i=0; i<4; i++) if (Value[GadNr(turn,i)] == -1) return(0);
  235.     return(1);
  236. }
  237. /***************************************************************************/
  238. int IsIn(BYTE a,BYTE *b)
  239. {
  240. int i;
  241.     for (i=0; i<4; i++) if(b[i] == a) return(1);
  242.     return(0);
  243. }
  244. /***************************************************************************/
  245. int WrightColor(int turn,BYTE *c)
  246. {
  247. int i,j,tot = 0,used;
  248.  
  249.     for (i=0; i<4; i++) 
  250.     {   /* for the @%! who used the same char twice... */
  251.         used = 0;
  252.         for (j=0; j<i; j++) 
  253.         {   if (Value[GadNr(turn,i)] == Value[GadNr(turn,j)]) 
  254.             {   used = 1;
  255.         }    }
  256.         if (!used)
  257.         {    if (IsIn(Value[GadNr(turn,i)],c))
  258.             {   tot++;
  259.     }   }    }   
  260.     return(tot);
  261.  
  262. }
  263. /***************************************************************************/
  264. int WrightPosition(int turn,BYTE *c)
  265. {
  266. int i,tot = 0;
  267.     for (i=0; i<4; i++) if (c[i] == Value[GadNr(turn,i)]) tot++;
  268.     return(tot);
  269. }
  270. /***************************************************************************/
  271. void ShowAnswer(struct RastPort *rp,BYTE *a)
  272. {
  273. int i;
  274.  
  275.     SetAPen(rp,0);
  276.     RectFill(rp,6,4,91,12);
  277.     for (i=1; i<5; i++) DrawGad(rp,0,i,a[i-1],1);
  278. }
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.